package com.bear.newsset.mvp.presen;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;

import com.bear.newsset.R;
import com.bear.newsset.dao.sql.NewsStatusSQL;
import com.bear.newsset.entity.OptEntity;
import com.bear.newsset.mvp.view.MeView;
import com.bear.newsset.ui.activity.NSApplication;
import com.bear.newsset.ui.activity.NewsManageActivity;
import com.bear.newsset.widget.FixedGridView;
import com.ybear.ybmvp.presenter.YBPresenter;

import java.util.ArrayList;
import java.util.List;

import static com.bear.newsset.dao.sql.NewsStatusSQL.NEWS_LIKE_TYPE.COLLECT;
import static com.bear.newsset.dao.sql.NewsStatusSQL.NEWS_LIKE_TYPE.GIVE_LIKE;
import static com.bear.newsset.dao.sql.NewsStatusSQL.NEWS_LIKE_TYPE.SHARE;

public class MePresen extends YBPresenter<MeView> {
    private NewsStatusSQL mStatusSQL;
    private String mAccount;
    private String[] gridTitle = { "历史", "收藏", "分享", "点赞", "获赞" };
    private List<FixedGridView.FixedGridEntity> mGridData;
    public MePresen(@NonNull MeView view) {
        super(view);
        init();
    }

    private void init() {
        if( getView() == null ) return;
        mStatusSQL = NewsStatusSQL.getInstance();
        mGridData = new ArrayList<>();
        //获取账号
        mAccount = ((NSApplication)((Activity)getView().getContext())
                .getApplication())
                .getAccountEntity()
                .getAccount();
    }

    public String getAccount() {
        return mAccount;
    }

    public void startNewsManageActivity(@IntRange(from = 0) int position) {
        if( getView() == null ) return;
        Context context = getView().getContext();

        Intent intent = new Intent(context, NewsManageActivity.class);
        String title = position < 0 || position >= gridTitle.length ? null : gridTitle[ position ];
        int[] types = { NewsManageActivity.TYPE_HISTORY, NewsManageActivity.TYPE_COLLECTION,
                NewsManageActivity.TYPE_SHARE, NewsManageActivity.TYPE_GIVE_LIKE };
        //默认为所有
        int type = position < 0 || position >= types.length ? -1 : types[ position ];
        //设置标题
        intent.putExtra(NewsManageActivity.TITLE_NAME, title);
        //设置管理类型
        intent.putExtra(NewsManageActivity.TYPE_NAME, type);
        //打开新闻管理页面
        context.startActivity( intent );
    }

    /**
     * 获取Opt列表的数据
     * @return      列表数据
     */
    public List<OptEntity> getOptData() {
        List<OptEntity> data = new ArrayList<>();
        /* Opt图标和标题 */
        int[] optImg = { R.drawable.ic_item_feedback, R.drawable.ic_item_setting };
        String[] optTitles = { "意见反馈", "设置"};
        //Opt数据
        for (int i = 0; i < optTitles.length; i++) {
            data.add(new OptEntity()
                    .setImgRes(optImg[ i ])
                    .setTitle(optTitles[ i ]));
        }
        return data;
    }

    /**
     * 更新Grid布局的喜欢状态的数量
     */
    public void updateGridActionCount() {
        long[] counts = {
                getCollectCount( mAccount ),
                getShareCount( mAccount ),
                getGiveLikeCount( mAccount )
        };
        for (int i = 1; i <= counts.length; i++) {
            mGridData.get( i ).setContent( String.valueOf(counts[ i - 1 ]) );
        }
    }

    /**
     * 设置Grid布局的数据
     */

    public void setGridActionData() {
        mGridData.clear();

        String collectCount, shareCount, likeCount;
        /* Grid图标、标题、内容 */
        int[] gridImg = {
                R.drawable.ic_grid_history, R.drawable.ic_grid_collection, R.drawable.ic_grid_share,
                R.drawable.ic_grid_like, R.drawable.ic_grid_get_like
        };
        //收藏的数量
        collectCount = String.valueOf( getCollectCount( mAccount ) );
        //分享的数量
        shareCount = String.valueOf( getShareCount( mAccount ) );
        //喜欢的数量
        likeCount = String.valueOf( getGiveLikeCount( mAccount ) );

        String[] gridContent = { "浏览足迹", collectCount, shareCount, likeCount, "尚未开放" };

        //Grid数据
        for (int i = 0; i < gridTitle.length; i++) {
            mGridData.add(new FixedGridView.FixedGridEntity()
                    .setImg(gridImg[i])
                    .setTitle(gridTitle[i])
                    .setContent(gridContent[i]));
        }
    }

    public List<FixedGridView.FixedGridEntity> getGridActionData() {
        return mGridData;
    }

    public long getCollectCount(String account) {
        return mStatusSQL.queryNewsLikeSqlCountForAccount( account, COLLECT );
    }

    public long getShareCount(String account) {
        return mStatusSQL.queryNewsLikeSqlCountForAccount( account, SHARE );
    }

    public long getGiveLikeCount(String account) {
        return mStatusSQL.queryNewsLikeSqlCountForAccount( account, GIVE_LIKE );
    }
}
